package com.xbfinance.portal.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xbfinance.portal.pojo.po.P2pProjectOrder;
import com.xbfinance.portal.pojo.po.P2pUser;
import com.xbfinance.portal.pojo.po.ProjectOrder;
import com.xbfinance.portal.service.OrderService;
import com.xbfinance.portal.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/addOrder")
    public String addOrder(Long projectid, HttpSession session, Double money, String paypass) {

        P2pUser user = (P2pUser) session.getAttribute("user");

        if(user!=null&& paypass!=null){

           P2pUser user1 =   userService.checkPaypass(user.getUserid(), paypass);

           if(user1!=null) {

               orderService.addOrder(projectid, user.getUserid(), money);

               return "index";
           }
        }

        return "index";
    }

    @RequestMapping(value = "/queryOrder")
    public String queryOrder(HttpSession session, Model model, @RequestParam(defaultValue = "3") Integer pageSize,
                             @RequestParam(defaultValue = "1") Integer currentPage) {
        P2pUser user = (P2pUser) session.getAttribute("user");

        if(user!=null) {

            Page<Object> page = PageHelper.startPage(currentPage, pageSize);


            List<ProjectOrder> list = orderService.queryOrder(user.getUserid());

            PageInfo<ProjectOrder> pageInfo = new PageInfo<>(list, 3);

            int[] nums = pageInfo.getNavigatepageNums();

            model.addAttribute("nums", nums);
            model.addAttribute("list", list);
            model.addAttribute("page", pageInfo);

            return "msim_my";

        }else{

            return "index";
        }
    }

    @RequestMapping(value = "/queryOrderByName")
    public String queryOrderByName(HttpSession session, Model model, @RequestParam(defaultValue = "3") Integer pageSize,
                                   @RequestParam(defaultValue = "1") Integer currentPage, String name){

        P2pUser user = (P2pUser) session.getAttribute("user");

        if(user!=null) {
            Page<Object> page = PageHelper.startPage(currentPage, pageSize);

            List<ProjectOrder> list = orderService.queryOrderByName(user.getUserid(),name);

            PageInfo<ProjectOrder> pageInfo = new PageInfo<>(list, 3);

            int[] nums = pageInfo.getNavigatepageNums();

            model.addAttribute("nums", nums);
            model.addAttribute("list", list);
            model.addAttribute("page", pageInfo);

            return "msim_my";
        }else{
            return "index";
        }
    }

}
